/* ---------------------------------------------------------------------


●　使用例(フリープラグインへセット)
<script type='text/javascript' src='http://stat100.ameba.jp/blog/js/apm001.js'></script>
<script>(function(j){j(function(){$=jQuery=j})})($)</script>
<script type='text/javascript' src='https://dl.dropboxusercontent.com/u/192658020/script/Paper_fortune.js'></script>
<script type="text/javascript">
// Paper_fortune_set おみくじ
// おみくじのオプション をここでセットします。（例）
var Paper_fortune_option=[{items:"大吉,中吉,小吉,吉,末吉,凶",items_Probability:"5,10,20,40,20,5",
items_img_FURL:"http://stat001.ameba.jp/user_images/20130827/04/new-bulue/",items_img_all:"28/23/g/o0200005012662981604.gif",items_img:"71/26/g/o0200005012662981639.gif,aa/8a/g/o0200005012662981652.gif,fc/13/g/o0200005012662981610.gif,93/fe/g/o0200005012662981655.gif,24/f1/g/o0200005012662981633.gif,7c/ba/g/o0200005012662986262.gif",run_btn_desc:"おみくじ",run_Result_message:"超ラッキー大吉,ラッキー中吉,少しラッキー小吉,吉ですよ,末吉だね,アンラッキー凶",retry_btn_desc:"リセット",test_mode:"OFF"}];
$(document).ready(function() {
$(".Paper_fortune_set_area").Paper_fortune_set();
});
</script>

●　表示した場所に下記を入れる（記事など　ＨＴＭＬモードで入れる）

<div class="Paper_fortune_set_area"></div>


----------------------------------------------------------------------- */


// --------------------------------------------------------------------
// おみくじの実行(Execution of Fortune)
// --------------------------------------------------------------------
$.fn.extend({
Paper_fortune_run:function(){
// option set
var Paper_fortune_o    = Paper_fortune_option[0];
var items              = Paper_fortune_o.items;
var items_Probability  = Paper_fortune_o.items_Probability;
var items_img_FURL     = Paper_fortune_o.items_img_FURL;
var items_img          = Paper_fortune_o.items_img;
var items_img_all      = Paper_fortune_o.items_img_all;
var run_Result_message = Paper_fortune_o.run_Result_message;
var test_mode          = Paper_fortune_o.test_mode;
// Array set
var items_Array        = items.split(",");                // 大吉～凶(Excellent luck ...... Misfortune)
var items_Prob_Array   = items_Probability.split(",");    // 確率(Probability)
var run_items_img_Array= items_img.split(",");            // 結果の画像(result image)
var run_Result_m_Array = run_Result_message.split(",");   // 結果のメッセージ(result message)
var items_Prob_Range_Array=new Array(), items_Prob_Summ=0;
for(i = 0; i < items_Prob_Array.length; i++){
items_Prob_Range_Array[i]=parseInt(items_Prob_Array[i])+items_Prob_Summ;
items_Prob_Summ=parseInt(items_Prob_Array[i])+items_Prob_Summ;
}
// テストモード(result display test_mode "ON")
if(test_mode=="ON"){
 var summary_display="";
 for(i = 0; i < items_Prob_Range_Array.length; i++){
  if(i!=0){
   summary_display+=items_Array[i]+" 範囲="+parseInt(items_Prob_Range_Array[i-1]+1)+"～"+items_Prob_Range_Array[i]+"\n";
  }else{
   summary_display+=items_Array[i]+" 範囲="+"1"+"～"+items_Prob_Range_Array[i]+"\n";
  }
 }
}
// 乱数を発生させる(Random number)
var rand_result        = Math.floor(Math.random()*100);
var result_index;
for(i = 0; i < items_Prob_Range_Array.length; i++){
 if(rand_result<=items_Prob_Range_Array[i]){
  result_index=i;break;
 }
}
// テストモード(result display test_mode "ON")
if(test_mode=="ON"){
 alert(summary_display+" rand_result="+rand_result+" index="+result_index+" items="+items_Array[result_index]);
}
// 結果を表示(Result set)
$("img.Paper_f_img",this).attr("src",items_img_FURL+run_items_img_Array[result_index]);
$(".Paper_f_msg",this).text(run_Result_m_Array[result_index]);
$("input[name='Paper_f_btn']",this).attr("disabled","disabled");
}
});

// --------------------------------------------------------------------
// おみくじを指定場所に表示する(will display in the location fortune)
// --------------------------------------------------------------------
$.fn.extend({
Paper_fortune_set:function(){
var Paper_fortune_o    = Paper_fortune_option[0];
var items_img_FURL     = Paper_fortune_o.items_img_FURL;
var items_img_all      = Paper_fortune_o.items_img_all;
var run_btn_desc       = Paper_fortune_o.run_btn_desc
var retry_btn_desc     = Paper_fortune_o.retry_btn_desc;
var test_mode          = Paper_fortune_o.test_mode;
// $(this)のクラス名を取得(Get the name of the class $(this))
var selectors=$(this).attr("class");
// Array set
$(this).append('<div class="Paper_fortune"><img class="Paper_f_img" src="'+items_img_FURL+items_img_all+'"><br><input type="button" name="Paper_f_btn" value="'+run_btn_desc+'" onClick="$(\'.'+selectors+'\').Paper_fortune_run();"> <input type="button" name="Paper_f_btn_r" value="'+retry_btn_desc+'" onclick="$(\'.'+selectors+'\').Paper_fortune_reset();"><br><span class="Paper_f_msg"></span></div>');
}
});
// --------------------------------------------------------------------
// おみくじ リセット(Fortune reset)
// --------------------------------------------------------------------
$.fn.extend({
Paper_fortune_reset:function(){
var Paper_fortune_o    = Paper_fortune_option[0];
var items_img_FURL     = Paper_fortune_o.items_img_FURL;
var items_img_all      = Paper_fortune_o.items_img_all;
$("img.Paper_f_img",this).attr("src",items_img_FURL+items_img_all);
$(".Paper_f_msg",this).text("");
$("input[name='Paper_f_btn']",this).removeAttr("disabled");
}
});

